$(function() {
	upload_file.init();
});

var upload_file = {
		init: function init(){
			this.prepareUploadify();
			this.bindDisplayFile();
		},
		prepareUploadify : function prepareUploadify(){
			var questions = $(".question");
			//console.log(questions);
			var question_id_ary = new Array();
			for (var i = 0; i < questions.length; i++) {
				var question_id = $(questions[i]).find(".question-id").text();
				var upload_file = $("#uploadify-file-"+question_id);
				//var file_list = $("#div-file-list-"+question_id);
				//console.log(file_list);
				question_id_ary.push(question_id);
				if($(questions[i]).hasClass("qt-shortanswer") || $(questions[i]).hasClass("qt-essay") || $(questions[i]).hasClass("qt-analytical")) {
					upload_file.uploadify({
						'method':'POST',
						'debug'	 : false,
						'buttonText'	: '点击上传',
						'fileTypeDesc': '选择图片',
						'buttonCursor'	: 'pointer',
						'uploader'	 : document.getElementsByTagName('base')[0].href + 'student/upload-uploadify/',
						//'uploader'	 : '../../admin/upload-uploadify',
						'swf'	 : document.getElementsByTagName('base')[0].href + 'resources/uploadify3.2.1/uploadify.swf',
						'multi'	 : false,
						'auto'	 : true,
						'height'	 : '26',
						'width'	 : '60',
						'requeueErrors'	: false, //队列上传出错，是否继续回滚队列，即反复尝试上传。默认：false
						'fileSizeLimit'	: '184320', // expects input in kb
						'cancelImage'	: document.getElementsByTagName('base')[0].href + 'resources/uploadify3.2.1/uploadify-cancel.png',
						overrideEvents:['onSelect'],
						onUploadProgress: function() {
							//$('#loader').show();
						},
						onUploadComplete: function(file) {
							//console.log('onUploadComplete file: ' + file);
							//$('#maincontent').load(location.href+' #maincontent > *');
							//$(".fade-file").modal('hide');
						},
						onUploadSuccess : function(file, data, response) {
				            //console.log('The file ' + file.name + ' was successfully uploaded with a response of ' + response + ':' + data);
				            //console.log('data: '+data);
				        	var fileurl = data.split("|")[0];
				        	var filename = data.split("|")[1];
				        	var cur_selector = this.wrapper.selector.toString();
				        	console.log(cur_selector);
				        	var question_id = cur_selector.substr(16,cur_selector.length);
				        	var file_list = $("#div-file-list-"+question_id);
				        	console.log(file_list);
			        		file_list.after("<a href=\"..\\" + fileurl + "\" class=\"diaplay-file display-content-file\" target=\"_blank\" data-url=\"" + fileurl + "\">"+file.name+"</a>&nbsp;&nbsp;&nbsp;&nbsp;");
			        		//this.uploadify('settings', 'buttonText', '继续上传');
				        	/*for (var i = 0; i < questions.length; i++) {
				        		var question_id = $(questions[i]).find(".question-id").text();
				        		var file_list = $("#div-file-list-"+question_id);
				        		for(var j = 0; i < question_id_ary.length; j++) {
				        			
				        		}
				        		
					        	console.log(file_list);
				        		file_list.after("<a href=\"..\\" + fileurl + "\" class=\"diaplay-file display-content-file\" target=\"_blank\" data-url=\"" + fileurl + "\">"+file.name+"</a>&nbsp;&nbsp;&nbsp;&nbsp;");
				        		upload_file.uploadify('settings', 'buttonText', '继续上传');
				        	}*/
						},
						onDialogOpen:function(){
							//alert(1);
						},
						onSelect: function(e, queueId, fileObj) {
							//alert(1);
							//alert($("#SWFUpload_1").parent().attr("id"));
						},
						onSelectError: function(file,errorCode,errorMsg) {
							alert(errorCode+" "+errorMsg);
							if(errorCode==-110){
								util.error("只能上传180M以下的文件。");
								return false;
							}
						},
						onUploadError: function(file,errorCode,errorMsg, errorString) {
//							console.log('onUploadError file: '+file);
//							console.log(errorCode);
//							console.log(errorMsg);
//							console.log(errorString);
							util.error(errorMsg);
						}
					});
				}
			}
		},
		
		bindDisplayFile : function bindDisplayFile(){
			$("#bk-conent-question-content").delegate(".diaplay-file","click",function() {
				window.open(location.protocol + "//" + location.host + "/" + $(this).attr("href"));
				e.preventDefault();
			});
		}
		
};